package org.zp.handler;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.omg.CORBA.Any;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.zp.dto.GetMyLogisticsDto;
import org.zp.pojo.ExpressInfo;
import org.zp.pojo.UserInfo;
import org.zp.service.ExpressInfoService;
import org.zp.util.Ajax;
import org.zp.vo.ExperssVo;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@RestController
public class MyLogisticsHandler {
@Resource
private ExpressInfoService e;
/**
 * 获得物流的列表
 * @param dto
 * @param req
 * @param resp
 */
@GetMapping("getMyLogistics.act")
private void getMyLogistics(GetMyLogisticsDto dto,HttpServletRequest req, HttpServletResponse resp) {
	UserInfo user = (UserInfo) req.getSession().getAttribute("user");
	Integer startpage = Integer.parseInt(dto.getStartpage());
	Integer limitpage = Integer.parseInt(dto.getLimitpage());
	
	Long check = (long) Integer.parseInt(dto.getCheck());
	String start = dto.getStart();//起始日期
	String end = dto.getEnd();//结束日期


if (check==0) {
	check = null;
}
if (start.equals("")) {
		start = null;
}
if (end.equals("")) {
	end = null;
}
if (start!=null&&end!=null) {
	DateFormat df = new SimpleDateFormat("yyyy-MM-dd");//日期格式处理
     try {
			Date star = df.parse(start);
			Date endd = df.parse(end);
			if (star.getTime()>endd.getTime()){//比较时间的大小
				Ajax.error(resp,"时间有误",10001);
				return;
         }
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
}
Integer uid = 0;
try {
	 uid = user.getuId();
} catch (Exception e) {
	Ajax.error(resp,"您的id不见了,请重新登录");
	return;
}
List<ExperssVo> list = e.getMyLogistics(check,start,end,uid,startpage,limitpage);
PageInfo<ExperssVo>pageInfo = new PageInfo<ExperssVo>(list);
Integer count = (int) pageInfo.getTotal();
System.out.println("多条件count:"+count);
Ajax.success(resp, "list","count", list,count);
}
/**
 * 用户退单操作
 * @param eid
 * @param req
 * @param resp
 */
@PostMapping("tuidan.act")
private void tuidan(String eid,HttpServletRequest req, HttpServletResponse resp) {
	Integer eidd = Integer.parseInt(eid);
	Integer flag = e.tuidan(eidd);
	if(flag>0) {
		System.out.println("保存成功");
		Ajax.success(resp);
		}else {
		Ajax.error(resp,"保存失败");
		}
}
}
